package student.aistar.day11.homework;

import java.util.*;

public class TestPurcase {
    public static void main(String[] args) {
        List<Purcase> list = new ArrayList<>();

        Purcase p1 = new Purcase("宝洁","洗手粉",18.5);
        Purcase p2 = new Purcase("联合利华","肥皂",4.5);
        Purcase p3 = new Purcase("宝洁","牙膏",32.5);
        Purcase p4 = new Purcase("宝洁","毛巾",14.5);
        Purcase p5 = new Purcase("洁利","洗面奶",26.0);
        Purcase p6 = new Purcase("好迪","洗发水",27.5);
        Purcase p7 = new Purcase("多芬","沐浴露",38.5);
        Purcase p8 = new Purcase("宝洁","洗洁精",3.4);

        list.add(p1);
        list.add(p2);
        list.add(p3);
        list.add(p4);
        list.add(p5);
        list.add(p6);
        list.add(p7);
        list.add(p8);

        countGroup2(list);
    }

    /**
     * 第一种方式 - 使用了第三方实体类封装了统计之后的结果
     *      对象的作用 - 数据在内存中的载体
     *      (这种方法的意思是, 把map里面的key, value抽取出一个类
     *      , key, value作为属性, 然后将map取出来放到类中,在List对类的属性排序)
     * @param list
     */
    public static void countGroup(List<Purcase> list) {
        //1.将每个品牌和这个品牌对应的对象封装到一块 - 归类
        //key - 品牌
        //value - 该品牌对应的所有的对象的结合
        Map<String, List<Purcase>> map = new HashMap<>();

        //2.遍历list
        for (Purcase purcase : list) {
            //获取品牌
            String brand = purcase.getBrand();
            //判断maps集合中是否已经包含了这个对象
            if(map.containsKey(brand)){
                //根据品牌名将这个对应的value获取出来
                List<Purcase> oldList = map.get(brand);
                oldList.add(purcase);
            }else{
                List<Purcase> pList = new ArrayList<>();
                pList.add(purcase);

                //放入到maps集合中
                map.put(brand, pList);
            }
        }
        //4.为排序准备的一个集合
        List<PurcaseSort> sortList = new ArrayList<>();

        //3.统计
        Set<String> keySet = map.keySet();
        Iterator<String> iterator = keySet.iterator();
        while (iterator.hasNext()){
            String brand = iterator.next();
            double total = 0.0d;

            //根据key获取value
            List<Purcase> countList = map.get(brand);

            for (Purcase cp : countList) {
                total += cp.getCost();
            }

            PurcaseSort purcaseSort = new PurcaseSort(brand, total);
            sortList.add(purcaseSort);
        }

        //排序
        sortList.sort((s1, s2)-> (int) (s2.getTotal() - s1.getTotal()));

        for (PurcaseSort purcaseSort : sortList) {
            System.out.println(purcaseSort.getBrand() + ": " + purcaseSort.getTotal());
        }
    }

    public static void countGroup2(List<Purcase> list){
        //1.将一个品牌和这个品牌对应的对象封装到一块,归类
        //key - 品牌
        //value -该品牌所有对象的集合
        Map<String, List<Purcase>> map = new HashMap<>();

        //2.遍历list
        for (Purcase purcase : list) {
            //获取品牌
            String brand = purcase.getBrand();
            //判断map集合中是否包含了这个brand
            if(map.containsKey(brand)){
                //根据品牌将这个value的获取出来
                List<Purcase> oldList = map.get(brand);
                oldList.add(purcase);
            }
            else{
                //遇到新的品牌的时候
                List<Purcase> newList = new ArrayList<>();
                newList.add(purcase);

                //放入到map中
                map.put(brand, newList);
            }
        }

        //封装统计后的结果, 不需要第三方实体类的结果

        //3.统计
        Map<String, Double> groupMap = new HashMap<>();
        Set<String> keySet = map.keySet();
        Iterator<String> iterator = keySet.iterator();
        while (iterator.hasNext()){
            String brand = iterator.next();
            double sum = 0.0;
            if(map.containsKey(brand)){
                List<Purcase> purcases = map.get(brand);
                for (int i = 0; i < purcases.size(); i++) {
                    sum += purcases.get(i).getCost();
                }
                groupMap.put(brand, sum);
            }
        }

        List<Map.Entry<String, Double>> sortList = new ArrayList<>();
        Set<Map.Entry<String, Double>> entrySet = groupMap.entrySet();
        Iterator<Map.Entry<String, Double>> iterator1 = entrySet.iterator();
        while(iterator1.hasNext()){
            Map.Entry<String, Double> next = iterator1.next();
            sortList.add(next);
        }

        sortList.sort((m1, m2) -> (int) (m1.getValue() - m2.getValue()));
        System.out.println(sortList);

    }
}
